#include "nsRegister_Test.h"

#include <iostream>
#include <string>
#include "nameserver/net/Register.h"
#include "nameserver/net/nsMsg.h"
/*********************************************************/
std::string nsRegisterTest::name()
{
	return("nsRegister");
}
/*********************************************************/
bool nsRegisterTest::runUnitTests()
{
	bool result = true;

	if ( !deserializeTest() ) result = false;

	return( result ) ;
}
/*********************************************************/
bool nsRegisterTest::deserializeTest()
{
	std::cout << "Running: deserializeTest()";
	std::cout.flush();

	try {
		std::string msg = "<GS><Register><hostaddr>localhost:4000</hostaddr><ringname>ring5001</ringname><ringaddr>localhost:5001</ringaddr></Register></GS>";
	
		nsMsg* packet = nsMsg::deserialize(msg);
		if ( packet ) {
			Register* reg = dynamic_cast<Register*>(packet);
			if ( reg ) {
				delete( packet );
				std::cout << "... succes!" << std::endl;
				return true;
			} else {
				std::cout << "... failed! Incorrect packet type parsed from a register message!" << std::endl;
				delete( packet);
				return false;
			}
		} else {
			std::cout << "... failed! Packet did not parse!" << std::endl;
			return false;
		}
	} catch( std::exception& e) {
		std::cout << "... failed! Caught exception: " << e.what() << std::endl;
		return false;
	} catch(...) {
		std::cout << "... failed! caught unknown exception, expected none!" << std::endl;
		return false;
	}
	std::cout << "... failed!" << std::endl;
	return false;
}
